package com.heima.controller;

import com.github.pagehelper.PageInfo;
import com.heima.domain.Product;
import com.heima.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private IProductService service;
    @RequestMapping("/findAll")
    public String findAll(Model model,
                          @RequestParam(name = "page",defaultValue = "1") Integer page,
                          @RequestParam(name = "pageSize",defaultValue = "3") Integer pageSize){
        List<Product> products = service.findAll(page,pageSize);
       /* for(Product product:products){
            System.out.println(product);
        }*/
        PageInfo<Product> pageInfo=new PageInfo<Product>(products);
      /*  System.out.println(products);

        System.out.println(pageInfo);*/
       model.addAttribute("productPageInfo",pageInfo);
        return  "product-list";
    }
    @RequestMapping("/save")
    public String  save(Product product) {
         service.save(product);
         return "redirect:findAll";

    }
    @RequestMapping("/deleleByIds")
    public String  deleleByIds(@RequestParam(name = "ids") String[] ids){
        System.out.println(Arrays.toString(ids));
        if(ids!=null&&ids.length!=0){
            for(String id:ids){
              service.deleteById(id);
            }
        }
        return "redirect:findAll";
    }
}
